/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author kien.tran
 */
public class LabelItem extends AFormItem {

    public LabelItem(String id, String title) {
        super(id, title);
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }

        // pre-calc
        calculateHeight();
        if (titleRows == null) {
            setTitleRows(title);
        }

        // paint background
        g.setColor(focus == false ? style.getBgColor() : style.getSelectedBgColor());
        g.fillRect(x, y, width, height);

        // paint title
        int i, tempX, tempY;
        if (title.length() != 0) {
            tempX = x + style.getPadding().getLeft() + titleStyle.getPadding().getLeft();
            for (i = 0; i < titleRows.length; i++) {
                tempY = y + style.getPadding().getTop() + i * (font.getFontBold().getHeight() + titleStyle.getPadding().getTop());
                font.getFontBold().drawString(g, titleRows[i], tempX, tempY, Graphics.TOP | Graphics.LEFT);
            }
        }
    }

    protected void calculateHeight() {
        if (titleRows == null) {
            setTitleRows(title);
        }

        if (title.length() > 0) {
            titleStyle.setHeight(titleRows.length * (font.getFontBold().getHeight() + titleStyle.getPadding().getTop()) + titleStyle.getPadding().getBottom());
        } else {
            titleStyle.setHeight(0);
        }

        this.height = titleStyle.getHeight() + style.getPadding().getTopAndBottom();
    }

    protected void setTitleRows(String title) {
        if (title.length() > 0) {
            int w = width - style.getPadding().getLeftAndRight() - titleStyle.getPadding().getLeftAndRight();
//            titleRows = boldFont.splitWith(title, w);
            titleRows = new String[]{title};
        } else {
            titleRows = new String[]{""};
        }
    }
}
